---@class Battle.EntityAttributeSystem:Battle.EntityComponent
local EntityAttributeSystem = Battle.CreateClass("EntityAttributeSystem",Battle.EntityComponent)
Battle.EntityAttributeSystem = EntityAttributeSystem
function EntityAttributeSystem:OnCreate()
    self.baseAttrList = {}  --基础属性
    self.attrList = {}  --实时属性
    self.oldList = {}
    --攻速万分比
    self.atkRateList = {}
    --移速万分比
    self.speedRateList = {}
    self.hp = 0
end
function EntityAttributeSystem:OnDestroy()
end
function EntityAttributeSystem:OnInitialized()
    self:CalcAllAttr(true)
end
function EntityAttributeSystem:CalcAllAttr(isInit)
    table.clear(self.atkRateList)
    table.clear(self.speedRateList)
    self:SaveOld()
    for k, v in pairs(self.baseAttrList) do
        if k ~= Battle.AttributeType.HP or isInit then
            self.attrList[k] = v
        end
        if k == Battle.AttributeType.ATK_RATE_PER then
            table.insert(self.atkRateList,v)
        end
        if k == Battle.AttributeType.SPEED_PER then
            table.insert(self.speedRateList,v)
        end
    end
    if isInit then
        self.attrList[Battle.AttributeType.HPMax] = self.attrList[Battle.AttributeType.HP]
    else
        local flag = false
        if table.getn(self.oldList) ~= table.getn(self.attrList) then
            flag = true
        else
            for k,v in pairs(self.oldList) do
                if v ~= self.attrList[k] then
                    flag = true
                    break
                end
            end
        end
        self:FireEvent()
    end
end
function EntityAttributeSystem:GetBaseAttr(attrId)
    return self.baseAttrList[attrId] or 0
end
function EntityAttributeSystem:SetBaseAttr(attrId,value)
    self.baseAttrList[attrId] = value
end
function EntityAttributeSystem:GetAttr(attrId)
    return self.attrList[attrId] or 0
end
function EntityAttributeSystem:SetAttr(attrId, value)
    self.attrList[attrId] = value
end
function EntityAttributeSystem:AddAttr(attrId,value,isSendEvent)
    if isSendEvent then self:SaveOld() end
    local old = self.attrList[attrId] or 0
    self.attrList[attrId] = old + value
    if attrId == Battle.AttributeType.ATK_RATE_PER then
        table.insert(self.atkRateList,value)
    end
    if attrId == Battle.AttributeType.SPEED_PER then
        table.insert(self.speedRateList,value)
    end
    local add =  attrId == Battle.AttributeType.HP and value or 0
    local per = attrId == Battle.AttributeType.HP_PER and value or 0
    if add ~= 0 or per ~= 0 then
        local per = per/10000
        self:SetHp(self:GetHp() + add + per*self:GetHpMax(),false)
        self.actor.eventSystem:FireEvent(Battle.Event.PlayerHpChanged,Battle.EventArgs.EntityHpChangeArgs:Create(self.actor))
    end
    if isSendEvent then
        self:FireEvent()
    end
end
function EntityAttributeSystem:RemoveAttr(attrId,value,isSendEvent)
    if isSendEvent then self:SaveOld() end
    local old = self.attrList[attrId] or 0
    self.attrList[attrId] = old - value
    if attrId == Battle.AttributeType.ATK_RATE_PER then
        table.RemoveValue(self.atkRateList,value,true)
    end
    if attrId == Battle.AttributeType.SPEED_PER then
        table.RemoveValue(self.speedRateList,value,true)
    end
    if isSendEvent then
        self:FireEvent()
    end
end
function EntityAttributeSystem:AddAttrList(attrList)
    self:SaveOld()
    for k,v in pairs(attrList) do
        self:AddAttr(k,v)
    end
    self:FireEvent()
end

function EntityAttributeSystem:RemoveAttrList(attrList)
    self:SaveOld()
    for k,v in pairs(attrList) do
        self:RemoveAttr(k,v)
    end
    self:FireEvent()
end
function EntityAttributeSystem:SaveOld()
    table.clear(self.oldList)
    for k,v in pairs(self.attrList) do
        self.oldList[k] = v
    end
end
function EntityAttributeSystem:FireEvent()
    self.actor.eventSystem:FireEvent(Battle.Event.ActorAttributeChange,Battle.EventArgs.EntityAttributeChangeArgs:Create(self.actor,self.oldList,self.attrList))
end
function EntityAttributeSystem:GetHp()
    return self:GetAttr(Battle.AttributeType.HP)
end
function EntityAttributeSystem:SetHp(hp,isSendEvent)
    if hp <= 0 then
        hp = 0
    end
    hp = math.min(hp, self:GetHpMax())
    self:SaveOld()
    self:SetAttr(Battle.AttributeType.HP, hp)
    if isSendEvent then
        self:FireEvent()
    end
end
function EntityAttributeSystem:GetHpMax()
    return self:GetAttr(Battle.AttributeType.HPMax)* (1+ self:GetAttr(Battle.AttributeType.HP_MAX_PER)/10000)
end
function EntityAttributeSystem:GetSpeed()
    return self:GetAttr(Battle.AttributeType.SPEED) / 1000
end
--攻击力
function EntityAttributeSystem:GetAtk()
    return self:GetAttr(Battle.AttributeType.ATK) * (1+ self:GetAttr(Battle.AttributeType.ATK_PER)/10000)
end
--防御
function EntityAttributeSystem:GetDef()
    return self:GetAttr(Battle.AttributeType.DEF) * (1+ self:GetAttr(Battle.AttributeType.DEF_PER)/10000)
end
--吸血
function EntityAttributeSystem:GetVampire(damage)
    local v = self:GetAttr(Battle.AttributeType.Vampire)
    if v > 0 then
        return v/10000 * damage
    end
   return 0
end